package com.bjpowernode.mvc.controller;

import cn.hutool.core.util.PhoneUtil;
import com.bjpowernode.common.enums.Code;
import com.bjpowernode.db.domain.User;
import com.bjpowernode.mvc.model.response.Response;
import com.bjpowernode.mvc.service.inter.SmsService;
import com.bjpowernode.mvc.service.inter.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*@CrossOrigin*/
@RestController
public class SmsController {

    @Resource
    private SmsService service;
    @Resource
    private UserService userService;
    //发送注册验证码
    @GetMapping(value = "/sms/reg")
    public Response sendRegSmsCode(@RequestParam String phone) {

        Response response = Response.FAIL(Code.PHONE_FORMAT_ERR);
        if (PhoneUtil.isPhone(phone)){
            User user = userService.queryByPhone(phone);
            if (user == null) {
                //判断手机号格式
                boolean result = service.handleRegisterCode(phone);

                response =result? Response.ok():Response.FAIL();
            }else {
             response=Response.FAIL(Code.PHONE_HAS_REGISTED);
         }
        }
        return  response;

    }

    //发送登录验证码
    @GetMapping("/sms/login")
    public Response sendLoginSmsCode(@RequestParam String phone){
        Response response=Response.FAIL(Code.PHONE_FORMAT_ERR);
        if (PhoneUtil.isPhone(phone)){
            User user=userService.queryByPhone(phone);
            if (user != null){
            //发送.
            boolean result=service.handleLoginCode(phone);
            response=result?Response.ok():Response.FAIL();
            }else {
                response= Response.FAIL(Code.PHONE_NO_REGISTED);
            }
        }
        return response;
    }
}
